﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using Abreu.Profile.Database;


namespace CorpAbreu.Common.Models
{
    public class GeneralDataSource
    {
        #region Class Members
        private List<BindData> listCountry;
        private List<BindData> listCity;
        private List<BindData> listConcel;
        private List<BindData> listDistrict;
        private List<BindData> listNuts;

        private CorpAbreuDataContext _corpAbreuDC = new CorpAbreuDataContext();
        #endregion

        #region Public Members
        /// <summary>
        /// Gets the Countries in DB
        /// </summary>
        /// <returns>Dictionary with Country ids and names</returns>
        public List<BindData> GetCountries()
        {
            listCountry = new List<BindData>();

            //Get countries with P_GetAllCountries_Sel
            var objCountry = _corpAbreuDC.P_GetAllCountries();

            foreach (P_GetAllCountriesResult objCountryItem in objCountry)
            {
                listCountry.Add(new BindData(objCountryItem.CountryId, objCountryItem.Name));
            }

            return listCountry;
        }

        /// <summary>
        /// Gets the Cities in DB by country id
        /// </summary>
        /// <returns>Dictionary with City ids and names</returns>
        public List<BindData> GetCities(int? countryId)
        {
            listCity = new List<BindData>();

            //Get cities with P_GetAllCitiesByCountryId_Sel
            var objCities = _corpAbreuDC.P_GetAllCitiesByCountryId(countryId);

            foreach (P_GetAllCitiesByCountryIdResult objCitiesItem in objCities)
            {
                listCity.Add(new BindData(objCitiesItem.CityId, objCitiesItem.Name));
            }

            return listCity;
        }

        /// <summary>
        /// Gets the Concelhos in DB
        /// </summary>
        /// <returns>Collection of Concelho names</returns>
        public List<BindData> GetConcel(int? distritoId)
        {
            listConcel = new List<BindData>();

            //Get concelhos with P_Concelho_sel
            var objConcelhos = _corpAbreuDC.P_GetConcelhoByDistrito(distritoId);

            foreach (P_GetConcelhoByDistritoResult objConcelhosItem in objConcelhos)
            {
                listConcel.Add(new BindData(objConcelhosItem.ConcelhoId, objConcelhosItem.Name));
            }

            return listConcel;
        }

        /// <summary>
        /// Gets the Districts in DB
        /// </summary>
        /// <returns>Collection of district names</returns>
        public List<BindData> GetDistricts()
        {
            listDistrict = new List<BindData>();

            //Get distritos with P_Distrito_sel
            var objDistritos = _corpAbreuDC.P_GetDistritoInfo();

            foreach (P_GetDistritoInfoResult objDistritosItem in objDistritos)
            {
                listDistrict.Add(new BindData(objDistritosItem.DistritoId, objDistritosItem.Name));
            }

            return listDistrict;
        }

        /// <summary>
        /// Gets the nuts codes for the autocomplete Textbox
        /// </summary>
        /// <param name="code"></param>
        /// <returns>List of strings</returns>
        public List<string> GetNutsCodes(string code)
        {
            List<String> listCodes = new List<String>();

            //Get codes with P_GetNutsByDescription_Sel
            var objCodes = _corpAbreuDC.P_GetNutsByCode(code);

            foreach (P_GetNutsByCodeResult objCodesItem in objCodes)
            {
                listCodes.Add(objCodesItem.Code);
            }

            return listCodes;
        }

        /// <summary>
        /// Gets the nuts names for the autocomplete Textbox
        /// </summary>
        /// <param name="name"></param>
        /// <returns>List of strings</returns>
        public List<string> GetNutsNames(string name)
        {
            List<String> listNames = new List<String>();

            //Get names with P_GetNutsByDescription_Sel
            var objNames = _corpAbreuDC.P_GetNutsByDescription(name);

            foreach (P_GetNutsByDescriptionResult objNamesItem in objNames)
            {
                listNames.Add(objNamesItem.Nuts3);
            }

            return listNames;
        }

        /// <summary>
        /// Gets city name by Id
        /// </summary>
        /// <param name="cityId"></param>
        /// <returns>String</returns>
        public String GetCityById(int? cityId)
        {
            String name = String.Empty;

            if (cityId != null)
            {
                //Get city with P_GetCityById_Sel
                var objCity = _corpAbreuDC.P_GetCityById(cityId);

                foreach (P_GetCityByIdResult objCityItem in objCity)
                {
                    name = objCityItem.Name;
                }
            }

            return name;
        }

        /// <summary>
        /// Gets country name by Id
        /// </summary>
        /// <param name="countryId"></param>
        /// <returns>String</returns>
        public String GetCountryById(int? countryId)
        {
            String name = String.Empty;

            if (countryId != null)
            {
                //Get country with P_GetCountryById
                var objCountry = _corpAbreuDC.P_GetCountryById(countryId);

                foreach (P_GetCountryByIdResult objCountryItem in objCountry)
                {
                    name = objCountryItem.Name;
                }
            }

            return name;
        }

        /// <summary>
        /// Gets distrito name by Id
        /// </summary>
        /// <param name="distritoId"></param>
        /// <returns>String</returns>
        public String GetDistritoById(int? distritoId)
        {
            String name = String.Empty;

            if (distritoId != null)
            {
                //Get distrito with P_DistritoById_Sel
                var objDistrito = _corpAbreuDC.P_GetDistritoById(distritoId);

                foreach (P_GetDistritoByIdResult objDistritoItem in objDistrito)
                {
                    name = objDistritoItem.Name;
                }
            }

            return name;
        }

        /// <summary>
        /// Gets concelho name by Id
        /// </summary>
        /// <param name="concelhoId"></param>
        /// <returns>String</returns>
        public String GetConcelhoById(int? concelhoId, int? distritoId)
        {
            String name = String.Empty;

            if (concelhoId != null && distritoId != null)
            {
                //Get concelho with P_GetConcelhoById
                var objConcelho = _corpAbreuDC.P_GetConcelhoById(concelhoId, distritoId);

                foreach (P_GetConcelhoByIdResult objConcelhoItem in objConcelho)
                {
                    name = objConcelhoItem.Name;
                }
            }

            return name;
        }

        /// <summary>
        /// Gets Nut name
        /// </summary>
        /// <returns>Collention of Nuts</returns>
        public String GetNutById(string nutCode)
        {
            String name = String.Empty;

            if (nutCode != String.Empty)
            {
                //Get nuts with P_GetNutById
                var objNut = _corpAbreuDC.P_GetNutById(nutCode);

                foreach (P_GetNutByIdResult objNutItem in objNut)
                {
                    name = objNutItem.Nuts3;
                }
            }
            return name;
        }

        /// <summary>
        /// Gets all Nuts
        /// </summary>
        /// <returns>Collection of Nuts</returns>
        public List<BindData> GetNuts()
        {
            listNuts = new List<BindData>();

            //Get nuts with P_GetAllNuts_Sel
            var objNuts = _corpAbreuDC.P_GetAllNuts();

            foreach (P_GetAllNutsResult objNutsItem in objNuts)
            {
                listNuts.Add(new BindData(objNutsItem.Code, objNutsItem.Nuts3));
            }

            listNuts.RemoveAll(IsEmptyValue);

            return listNuts;
        }

        /// <summary>
        /// Gets all Nuts
        /// </summary>
        /// <returns>Collection of Nuts with concatenated code + name</returns>
        public List<BindData> GetNutsConcatenated()
        {
            listNuts = new List<BindData>();

            //Get nuts with P_GetAllNuts_Sel
            var objNuts = _corpAbreuDC.P_GetAllNuts();

            foreach (P_GetAllNutsResult objNutsItem in objNuts)
            {
                listNuts.Add(new BindData(objNutsItem.Code, objNutsItem.Nuts3, objNutsItem.Code + " - " + objNutsItem.Nuts3));
            }

            listNuts.RemoveAll(IsEmptyValue);

            return listNuts;
        }
        #endregion

        #region Private Members
        // Search predicate returns true if a string is empty.
        private static bool IsEmptyValue(BindData listItem)
        {
            if (listItem.Name == null)
            {
                return true;
            }
            else
            {
                return false;
            }
        } 
        #endregion
    }
}
